﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;
using ChatData;

namespace ChatServerOptions
{
    /// <summary>
    /// Static class for generating TCP replies to Clients
    /// </summary>
    class Reply
    {
        /// <summary>
        /// Sent client notification of file waiting
        /// </summary>
        public static bool SendNotificationFile(string IP, int port, string GUID){
            TcpClient tcp = new TcpClient();
            tcp.Connect(Utils.getIPAddress(IP), port);
            Stream stream = tcp.GetStream();
            PutString("File", stream);
            PutString(GUID, stream);
            stream.Flush();
            return true;
        }
        /// <summary>
        /// Sent client notification of DM waiting
        /// </summary>
        public static bool SendNotificationDM(string IP, int port, string username)
        {
            TcpClient tcp = new TcpClient();
            tcp.Connect(Utils.getIPAddress(IP), port);
            Stream stream = tcp.GetStream();
            PutString("DM", stream);
            PutString(username, stream);
            stream.Flush();
            return true;
        }
        /// <summary>
        /// Sent all clients notification of status change
        /// </summary>
        public static bool NotifyAllStatus(ActiveUserStore<Record> users, string username)
        {
            foreach (string user in users.AllUsers())
            {
                if (user != username)
                {
                    TcpClient tcp = new TcpClient();
                    tcp.Connect(Utils.getIPAddress(users.getIP(user)), users.getPort(user));
                    Stream stream = tcp.GetStream();
                    PutString("Status", stream);
                    PutString(username, stream);
                    stream.Flush();
                    tcp.Close();
                }
            }
            return true;
        }
        /// <summary>
        /// Sent all clients notification of Logoff
        /// </summary>
        public static bool NotifyAllLogOff(ActiveUserStore<Record> users, string username)
        {
            
            foreach (string user in users.AllUsers())
            {
                if (user != username)
                {
                    TcpClient tcp = new TcpClient();
                    tcp.Connect(Utils.getIPAddress(users.getIP(user)), users.getPort(user));
                    Stream stream = tcp.GetStream();
                    PutString("Logoff", stream);
                    PutString(username, stream);
                    stream.Flush();
                    tcp.Close();
                }
            }
            return true;
        }
        /// <summary>
        /// Sent all clients notification of Login
        /// </summary>
        public static bool NotifyAllLogIn(ActiveUserStore<Record> users, string username)
        {
            
            foreach (string user in users.AllUsers())
            {
                if (user != username)
                {
                    TcpClient tcp = new TcpClient();
                    tcp.Connect(Utils.getIPAddress(users.getIP(user)), users.getPort(user));
                    Stream stream = tcp.GetStream();

                    PutString("Login", stream);
                    PutString(username, stream);
                    stream.Flush();
                    tcp.Close();
                }
            }
            return true;
        }
        /// <summary>
        /// Put a string on the stream. String message. Stream to write to.
        /// </summary>
        static void PutString(string text, Stream stream)
        {
            byte[] bytes = Encoding.ASCII.GetBytes(text);
            int n = bytes.Length;
            stream.WriteByte((byte)n);
            stream.Write(bytes, 0, n);
        }

    }
}
